﻿$(function () {

    var $leftnavigation = $("#leftnavigation");
    $("#leftnavigationcontent").remove();

    // If cookie exist,
    // Read the cookie and get the role.
    // Then populate the navigation bar according to the role.
    // Or find out the role and populate the navigation bar according to the role.

    var cookieUserExist = $.cookie("UsersRole") || null;
    if (cookieUserExist === null || cookieUserExist === "" || cookieUserExist === "null" || cookieUserExist === undefined) {
        window.location.assign("/Login.aspx");
    } else {
        if (cookieUserExist === "roleId=5") {
            // Load Masterpage Handlebars
            var compiledMasterpage = Handlebars.getTemplate('storeclerk');
            var htmlMasterpage = compiledMasterpage();
            $(htmlMasterpage).prependTo($leftnavigation);
        } else if (cookieUserExist === "roleId=6") {
            // Load Masterpage Handlebars
            var compiledMasterpage = Handlebars.getTemplate('storesupervisor');
            var htmlMasterpage = compiledMasterpage();
            $(htmlMasterpage).prependTo($leftnavigation);
        } else if (cookieUserExist === "roleId=7") {
            // Load Masterpage Handlebars
            var compiledMasterpage = Handlebars.getTemplate('storemanager');
            var htmlMasterpage = compiledMasterpage();
            $(htmlMasterpage).prependTo($leftnavigation);
        } else if (cookieUserExist === "roleId=1" || cookieUserExist === "roleId=2") {
            // Load Masterpage Handlebars
            var compiledMasterpage = Handlebars.getTemplate('deptemployee');
            var htmlMasterpage = compiledMasterpage();
            $(htmlMasterpage).prependTo($leftnavigation);
        } else if (cookieUserExist === "roleId=3") {
            // Load Masterpage Handlebars
            var compiledMasterpage = Handlebars.getTemplate('actingmanager');
            var htmlMasterpage = compiledMasterpage();
            $(htmlMasterpage).prependTo($leftnavigation);
        } else if (cookieUserExist === "roleId=4") {
            // Load Masterpage Handlebars
            var compiledMasterpage = Handlebars.getTemplate('depthead');
            var htmlMasterpage = compiledMasterpage();
            $(htmlMasterpage).prependTo($leftnavigation);
        }
        else {
            window.location.assign("/Login.aspx");
        }
    }
});
